package com.cheyun.netsalev3.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.Nullable
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.databinding.library.baseAdapters.BR
import androidx.lifecycle.ViewModel
import androidx.recyclerview.widget.*
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.bean.MoreLayoutDefaultAdapterParam

/**
 * @author Michael
 * @date  2018/12/25 - 17:01.
 * 多布局适配器
 */
class MoreLayoutDefaultAdapter(var list: List<MoreLayoutDefaultAdapterParam>, var viewModel: ViewModel? = null) : RecyclerView.Adapter<MoreLayoutDefaultAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        for (i in 0 until list.size) {
            if (list[i].viewType == viewType) {
                return ViewHolder(DataBindingUtil.inflate(LayoutInflater.from(parent.context), list[i].layoutId, parent, false))
            }
        }
        return ViewHolder(DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.blank, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        var param = getItem(position)
        holder.apply {
            bind(param, viewModel)
            itemView.tag = param
        }
    }

    override fun getItemViewType(position: Int): Int {
        var item = mHelper.currentList[position]
        for (i in 0 until list.size) {
//        正式
            var param = list[i]
            if (param.beanMode == item::class) {
                list[i].viewType = i
                return i
            }
        }
        return -1

//        //测试
//        var i=position % list.size
//        list[i].viewType=i
//        return i

    }

    var mHelper: AsyncListDiffer<Any>


    init {
        mHelper = AsyncListDiffer(AdapterListUpdateCallback(this),
                AsyncDifferConfig.Builder(ItemCallback()).build())
    }

    fun submitList(@Nullable list: List<Any>) {
        mHelper.submitList(list)
    }

    fun getItem(position: Int): Any {
        return mHelper.currentList[position]
    }

    override fun getItemCount(): Int {
        return mHelper.currentList.size
    }

    class ViewHolder(var binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: Any, viewModel: ViewModel?) {
            binding.apply {
                setVariable(BR.item, item)
                setVariable(BR.viewModel, viewModel)
                setVariable(BR.binding, this)
                setVariable(BR.position, adapterPosition)
                executePendingBindings()
            }
        }
    }

    /**
     * 默认的比较器 可以覆盖比较方法
     */
    class ItemCallback : DiffUtil.ItemCallback<Any>() {
        override fun areItemsTheSame(oldItem: Any, newItem: Any): Boolean {
            return oldItem == newItem
        }

        override fun areContentsTheSame(oldItem: Any, newItem: Any): Boolean {
            return oldItem == newItem
        }

    }


}